package main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DKUtil {
	public static List<Integer> generatePrimes(int maxNumber) {
		List<Integer> primes = new ArrayList<>();

		boolean[] isPrime = new boolean[maxNumber + 1];

		Arrays.fill(isPrime, true);

		// mark non-primes <= N using Sieve of Eratosthenes
		for (int i = 2; i * i <= maxNumber; i++) {

			// if i is prime, then mark multiples of i as nonprime
			// suffices to consider mutiples i, i+1, ..., N/i
			if (isPrime[i]) {
				for (int j = i; i * j <= maxNumber; j++) {
					isPrime[i * j] = false;
				}
			}
		}

		for (int number = 2; number <= maxNumber; number++) {
			if (isPrime[number]) {
				primes.add(number);
			}
		}

		return primes;
	}

	public static int[] makeIntArray(List<Integer> primes) {
		int[] array = new int[primes.size()];
		int index = 0;
		for (Integer number : primes) {
			array[index] = number;
			index++;
		}
		return array;
	}

	public static int rebuildNumber(int[] primes) {
		int result = 1;
		for (int prime : primes) {
			result *= prime;
		}
		return result;
	}
}
